<?php

/**---
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: upgrade.php 29521 2012-04-17 09:24:42Z songlixin $
 *	Modified by Valery Votintsev, codersclub.org
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$sql = '';

$sql .= <<<EOF

CREATE TABLE IF NOT EXISTS pre_connect_tthreadlog (
  twid varchar(16) NOT NULL,
  tid int(11) unsigned NOT NULL DEFAULT '0',
  conopenid varchar(128) NOT NULL,
  pagetime int(11) unsigned DEFAULT '0',
  lasttwid varchar(16) DEFAULT NULL,
  nexttime int(11) unsigned DEFAULT '0',
  updatetime int(11) unsigned DEFAULT '0',
  dateline int(11) unsigned DEFAULT '0',
  PRIMARY KEY (twid),
  KEY nexttime (tid,nexttime),
  KEY updatetime (tid,updatetime)
) TYPE=MyISAM;

CREATE TABLE IF NOT EXISTS pre_common_connect_guest (
  `conopenid` varchar(128) NOT NULL default '',
  `conuin` varchar(64) NOT NULL default '',
  `conuinsecret` varchar(64) NOT NULL default '',
  `conqqnick` varchar(128) NOT NULL default '',
  PRIMARY KEY (conopenid)
) TYPE=MyISAM;

CREATE TABLE IF NOT EXISTS `pre_connect_disktask` (
 `taskid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Task ID',
 `aid` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Attachment ID',
 `uid` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'User ID',
 `openid` varchar(128) NOT NULL DEFAULT '' COMMENT 'openId',
 `filename` varchar(255) NOT NULL DEFAULT '' COMMENT 'Attachment Name',
 `verifycode` varchar(32) NOT NULL DEFAULT '' COMMENT 'Download verification code',
 `status` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT 'Download status',
 `dateline` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Add task time',
 `downloadtime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Download completion time',
 `extra` text COMMENT 'Reserved field',
 PRIMARY KEY (`taskid`),
 KEY `openid` (`openid`),
 KEY `status` (`status`)
) TYPE=MyISAM COMMENT='Site download task table';

EOF;

runquery($sql);

$sql = '';

// QQ秀字段
$columnexisted = false;

$query = DB::query("SHOW COLUMNS FROM ".DB::table('common_member_connect'));
while($temp = DB::fetch($query)) {
	if($temp['Field'] == 'conisqqshow') {
		$columnexisted = true;
		break;
	}
}
$sql .= !$columnexisted ? "ALTER TABLE ".DB::table('common_member_connect')." ADD COLUMN conisqqshow tinyint(1) unsigned NOT NULL default '0';\n" : '';

// QQ昵称字段
$query = DB::query("SHOW COLUMNS FROM ".DB::table('common_connect_guest'));
while($row = DB::fetch($query)) {
	if($row['Field'] == 'conqqnick') {
		$qqnickexisted = true;
		break;
	}
}
$sql .= !$qqnickexisted ? "ALTER TABLE ".DB::table('common_connect_guest')." ADD COLUMN conqqnick char(100) NOT NULL default '';\n" : '';

if($sql) {
	runquery($sql);
}

// 创建QQ互联快速登录用户组
$connect = C::t('common_setting')->fetch('connect', true);

// 微博回流字段
if (!array_key_exists('reply', $connect['t'])) {
	$connect['t']['reply'] = 1;
}
if (!array_key_exists('reply_showauthor', $connect['t'])) {
	$connect['t']['reply_showauthor'] = 1;
}

$needCreateGroup = false;
// 检测用户组是否存在
if ($connect['guest_groupid']) {
	$group = C::t('common_usergroup')->fetch($connect['guest_groupid']);
	if (!$group) {
		$needCreateGroup = true;
	}
} else {
	$needCreateGroup = true;
}

$newConnect = array();
include DISCUZ_ROOT . 'source/language/lang_admincp_cloud.php';
$name = $extend_lang['connect_guest_group_name'];
// 创建用户组
if ($needCreateGroup) {
	$userGroupData = array(
		'type' => 'special',
		'grouptitle' => $name,
		'allowvisit' => 1,
		'color' => '',
		'stars' => '',
	);
	$newGroupId = C::t('common_usergroup')->insert($userGroupData, true);

	// 用户组权限
	$dataField = array(
		'groupid' => $newGroupId,
		'allowsearch' => 2, // 允许搜索论坛
		'readaccess' => 1, // 阅读权限
		'allowgetattach' => 1, // 下载附件
		'allowgetimage' => 1, // 下载图片
	);
	C::t('common_usergroup_field')->insert($dataField);

	// 保存用户组
	$newConnect = array('guest_groupid' => $newGroupId);
	updatecache('usergroups');
}

// 合并更新字段
$updateData = array_merge($connect, $newConnect);
C::t('common_setting')->update('connect', serialize($updateData));
updatecache('setting');
$finish = true;
